package com.pig4cloud.pigx.common.customize.mysql.utils;

import com.pig4cloud.pigx.common.customize.mysql.dto.ExportExcelDTO;
import lombok.AllArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;

/**
 * Excel导入导出工具类
 *
 */
@Component
@AllArgsConstructor
public class ExportExcelUtils {

	public void init(ExportExcelDTO dto, HSSFWorkbook wb, HSSFSheet sheet) {
		HSSFCell cell;

		// 设置列宽
		for (int i = 0; i < dto.getColumnWidth().size(); i++) {
			sheet.setColumnWidth(i, dto.getColumnWidth().get(i) * 36);
		}

		// 第一行标题
		HSSFRow row = sheet.createRow(0);
		row.setHeight((short) (30 * 16));
		// 合并第一行 从第一行开始第一行结束，第0列开始 第11列结束
		CellRangeAddress callRangeAddress = new CellRangeAddress(0, 0, 0, dto.getColumnWidth().size() - 1);
		sheet.addMergedRegion(callRangeAddress);
		// 在第一行第 一个单元格
		cell = row.createCell(0);
		cell.setCellValue(dto.getHeaderTitle());
		// 第一行表头样式
		HSSFCellStyle firstRowStyle = wb.createCellStyle();
		HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 20);
		font.setFontName("宋体");
		firstRowStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		firstRowStyle.setAlignment(HorizontalAlignment.CENTER);
		firstRowStyle.setFont(font);
		cell.setCellStyle(firstRowStyle);

		// 第二行日期
		HSSFRow rowTwo = sheet.createRow(1);
		rowTwo.setHeight((short) (30 * 16));
		// 合并第二行，从第0列开始 cellNum 列结束
		CellRangeAddress cellRangeAddressTwo = new CellRangeAddress(1, 1, 0, dto.getColumnWidth().size() - 1);
		sheet.addMergedRegion(cellRangeAddressTwo);
		cell = rowTwo.createCell(0);
		// 第二行的合并内容
		cell.setCellValue("统计时间: " + dto.getHeaderTime());
		// 第二行日期样式
		HSSFCellStyle twoRowStyle = wb.createCellStyle();
		HSSFFont twoFont = wb.createFont();
		twoFont.setFontHeightInPoints((short) 14);
		twoFont.setFontName("宋体");
		twoRowStyle.setFont(twoFont);
		twoRowStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		twoRowStyle.setAlignment(HorizontalAlignment.RIGHT);
		cell.setCellStyle(twoRowStyle);

		// 第三行 遍历字段名
		HSSFRow title = sheet.createRow(2);
		for (int i = 0; i < dto.getColumnLabel().size(); i++) {
			cell = title.createCell(i);
			// 第二行的合并内容
			cell.setCellValue(dto.getColumnLabel().get(i));
			// 第三行字段名称样式
			HSSFCellStyle titleRowStyle = getCellStyle(wb, IndexedColors.GREY_25_PERCENT.index, (short) 11, true);
			cell.setCellStyle(titleRowStyle);
		}
	}

	/**
	 * 获取数据单元格样式
	 *
	 * @param wb     wb
	 * @param color  颜色 -> IndexedColors
	 * @param size   字号
	 * @param isBold 是否字体加粗
	 * @return HSSFCellStyle
	 */
	public static HSSFCellStyle getCellStyle(HSSFWorkbook wb, Short color, Short size, boolean isBold) {
		HSSFCellStyle labelStyle = wb.createCellStyle();
		labelStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		labelStyle.setAlignment(HorizontalAlignment.CENTER);
		if (color != null) {
			labelStyle.setFillForegroundColor(color);
			labelStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		}
		labelStyle.setBorderTop(BorderStyle.THIN);
		labelStyle.setBorderBottom(BorderStyle.THIN);
		labelStyle.setBorderRight(BorderStyle.THIN);
		labelStyle.setBorderLeft(BorderStyle.THIN);
		labelStyle.setWrapText(true);
		HSSFFont font = wb.createFont();
		font.setFontName("宋体");
		font.setBold(isBold);
		font.setFontHeightInPoints(size);
		labelStyle.setFont(font);
		return labelStyle;
	}

}
